let express = require('express');
let path = require('path');
let fs = require('fs');
let app = express();

// 记录每次请求的时间、请求方式和发起请求的路径
app.use(middleWear);
app.get('/',(req,res) => {
    res.send('<h1>这是首页</h1>');
})

app.post('/sendmessage',(req,res) => {
    res.send('你发起了post请求')
})

app.listen(8001,() => {
    console.log('服务器开始运行了：8001')
})

// 中间件
function middleWear(req,res,next){
    let message = `在${(new Date()). toLocaleString()}通过${req.method}方式发起了${req.url}请求\n`;
    fs.appendFile(path.join(__dirname,'status.txt'),message,(err) => {
        if(!err) console.log('写入成功！！！');
    });
    next();
}